#pragma once

#include <QWidget>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <connectdialog.h>
#include <downloadfiledialog.h>
#include <algorithm>
#include <logicsystem.h>
#include <msg_node.h>
#include <session.h>
#include "ui_filedownload.h"

//constexpr auto FILE_PATH = "../res/file_download";

enum class RecvState {
	RECV_HEADER = 0,
	RECV_BODY
};


QT_BEGIN_NAMESPACE
namespace Ui { class FileDownloadClass; };
QT_END_NAMESPACE

class FileDownload : public QWidget
{
	Q_OBJECT

public:
	FileDownload(QWidget *parent = nullptr);

	void init();

	/// <summary>
	/// 发送获取file tree请求
	/// </summary>
	void flushFileTree();

	~FileDownload();
private:
	/// <summary>
	/// 更新file tree
	/// </summary>
	/// <param name="fileTreeObj"></param>
	void updateFileTree(Json::Value& fileTreeObj);

public slots:

	void onConnectBtnClick();

	void onSigUpdateRecvData(const QString& data);
	
	void onDownloadBtnClick();

	void onUploadBtnClick();

	void onClearBtnClick();

	void onSendBtnClick();

	void onReadyRead();

private:
	Ui::FileDownloadClass *ui;

private:
	std::mutex recvLocker_;
	QString downloadPath_;
	std::shared_ptr<Session> session_;
	ConnectDialog* connectDialog_;
	DownloadFileDialog* downloadFileDialog_;
	//char buffer_[MSG_MAX_DATA_LEN];
	std::shared_ptr<MsgNode> recvHeader_;
	std::shared_ptr<RecvNode> recvBody_;
	RecvState recvState_;
	size_t fileRecvLen_; // 用于大文件传输时，标记文件的已接收的字节数
	size_t fileTotalLen_; // 用于大文件传输时，标记文件的总字节数
};
